<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch" label-width="68px">
      <el-form-item label="登录地址" prop="like.ipaddr">
        <el-input v-model="searchForm.like.ipaddr" placeholder="请输入登录地址" clearable class="w-160px"/>
      </el-form-item>
      <el-form-item label="用户名称" prop="like.login_name">
        <el-input v-model="searchForm.like.login_name" placeholder="请输入用户名称" clearable class="w-160px"/>
      </el-form-item>
      <el-form-item label="状态" prop="where.status">
        <el-select v-model="searchForm.where.status" placeholder="登录状态" clearable class="w-140px">
          <el-option v-for="dd in dict.type.sys_common_status" :key="dd.value" :label="dd.label" :value="dd.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间" prop="time.create_time">
        <el-date-picker
          v-model="searchForm.time.create_time"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="reset mb-8px">
      <el-button
        type="danger"
        plain
        icon="el-icon-delete"
        :disabled="!ids.length"
        @click="handleDelete"
        v-hasPerm="['system:login-log:delete']"
      >删除</el-button>
      <el-button
        type="danger"
        plain
        icon="el-icon-delete"
        @click="handleClean"
        v-hasPerm="['system:login-log:clear']"
      >清空</el-button>
      <el-button
        type="primary"
        plain
        icon="el-icon-unlock"
        :disabled="ids.length !== 1"
        @click="handleUnlock"
        v-hasPerm="['system:login-log:unlock']"
      >登出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList" />
    </div>
    <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelection"  @sort-change="handleSort">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="编号" width="60" align="center" prop="id" />
      <el-table-column label="用户名称" min-width="100" align="center" prop="login_name" show-overflow-tooltip sortable="custom"/>
      <el-table-column label="登录地址" align="center" prop="ipaddr" width="130" show-overflow-tooltip />
      <el-table-column label="登录地点" align="center" prop="location" show-overflow-tooltip />
      <el-table-column label="浏览器" align="center" prop="browser" show-overflow-tooltip />
      <el-table-column label="平台" align="center" prop="platform" show-overflow-tooltip />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登录状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作信息" align="center" prop="msg" show-overflow-tooltip />
      <el-table-column label="登录日期" align="center" prop="create_time" sortable="custom" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="searchForm.pageNum"
      :limit.sync="searchForm.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listLoginLogApi, delLoginLogApi, clearLoginLogApi, unlockLoginApi } from '@/api/system/login-log'

export default {
  name: "LoginLog",
  dicts: ['sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        like: { login_name: undefined, ipaddr: undefined },
        where: { status: undefined },
        time: { create_time: undefined },
        orderByColumn: 'id',
        orderBySort: 'desc',
      }
    };
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listLoginLogApi(this.searchForm).then(res => {
          this.list = res.list
          this.total = res.total
          this.loading = false
        }
      )
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },
    reset() {
      this.resetForm('searchFormRef')
      this.search()
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.id)
    },
    handleSort(column) {
      this.searchForm.orderByColumn = column.prop
      this.searchForm.orderBySort = column.order
      this.getList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id || this.ids.join(',')
      if (!infoIds) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认删除访问编号为"' + infoIds + '"的数据项？').then(function() {
        return delLoginLogApi(infoIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal.confirm('是否确认清空所有登录日志数据项？').then(function() {
        return clearLoginLogApi()
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('清空成功')
      }).catch(() => {
      })
    },
    /** 登出按钮操作 */
    handleUnlock() {
      const id = this.ids.length ? this.ids[0] : ''
      if (!id) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认登出编号为"' + id + '"用户?').then(function() {
        return unlockLoginApi(id)
      }).then(() => {
        this.$modal.msgSuccess('登出成功')
      }).catch(() => {
      })
    }
  }
};
</script>

